package livesun.justiceeternal.view.adapter.wrapper;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;

/**
 * Created by livesun on 2017/8/22.
 */

/**
 * 类描述：具有头部底部功能的RecyclerView
 * 创建人：livesun
 * 创建时间：2017/8/22
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */
public class WrapperRecyclerView extends RecyclerView {

    private WrapperHeadAndFootAdapter mWrapperAdapter;
    private View mEmptyView, mLoadingView;
    //头部的key的起始值
    private static int HEAD_KEY=1000000;
    //脚部的key的起始值
    private static int FOOT_KEY=2000000;
    //头部的集合
    private SparseArray<View> mHeads=new SparseArray<>();
    //脚部的集合
    private SparseArray<View> mFoots=new SparseArray<>();
    private Adapter mAdapter;

    public WrapperRecyclerView(Context context) {
        this(context,null);
    }

    public WrapperRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public WrapperRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    @Override
    public void setAdapter(Adapter adapter) {
        if (mAdapter != null) {
            mAdapter.unregisterAdapterDataObserver(mDataObserver);
            mAdapter = null;
        }
        mAdapter = adapter;
        mAdapter.notifyDataSetChanged();
        mWrapperAdapter = new WrapperHeadAndFootAdapter(adapter,mHeads,mFoots);
        super.setAdapter(mWrapperAdapter);
        //适配GridLayoutManager
        mWrapperAdapter.spanSize(this);
        //注册监听器
        mWrapperAdapter.registerAdapterDataObserver(mDataObserver);
    }

    /**
     *添加头布局
     * @param view
     */
    public WrapperRecyclerView addHeadView(View view){
        if(mHeads.indexOfValue(view)==-1){
            int position = HEAD_KEY++;
            mHeads.put(position,view);
        }
        return this;
    }

    /**
     * 添加底布局
     * @param view
     */
    public WrapperRecyclerView addFootView(View view){
        if(mFoots.indexOfValue(view)==-1){
            mFoots.put(FOOT_KEY++,view);
        }
        return this;
    }


    /**
     * 设置头部的点击事件
     * @param onHeadClickListener
     */
    public WrapperRecyclerView setOnHeadClickListener(OnHeadClickListener onHeadClickListener){
        if(mWrapperAdapter!=null){
            mWrapperAdapter.setOnHeadClickListener(onHeadClickListener);
        }
        return this;
    }

    /**
     * 设置底部的点击事件
     */

    public WrapperRecyclerView setOnFootClickListener(OnFootClickListener onFootClickListener){
        if(mWrapperAdapter!=null){
            mWrapperAdapter.setOnFootClickListener(onFootClickListener);
        }
        return this;
    }


    /**
     * 移除头部
     * @param view
     */
    public WrapperRecyclerView removeHeadView(View view){
        if(mWrapperAdapter!=null){
            mWrapperAdapter.removeHeadView(view);
        }

        return this;
    }

    public WrapperRecyclerView removeHeadView(View view,int position){
        if(mWrapperAdapter!=null){
            mWrapperAdapter.removeHeadView(view,position);
        }
        return this;
    }
    /**
     * 移除脚部
     * @param view
     */
    public WrapperRecyclerView removeFootView(View view){
        if(mWrapperAdapter!=null){
            mWrapperAdapter.removeFootView(view);
        }
        return this;
    }

    public WrapperRecyclerView removeFootView(View view,int position){
        if(mWrapperAdapter!=null){
            mWrapperAdapter.removeFootView(view,position);
        }
        return this;
    }

    /**
     * 添加一个空列表数据页面
     */
    public void addEmptyView(View emptyView) {
        this.mEmptyView = emptyView;
    }

    /**
     * 添加一个正在加载数据的页面
     */
    public void addLoadingView(View loadingView) {
        this.mLoadingView = loadingView;
    }

    /**
     * Adapter数据改变的方法
     */
    private void dataChanged() {
        if (mAdapter.getItemCount() == 0) {
            // 没有数据
            if (mEmptyView != null) {
                mEmptyView.setVisibility(VISIBLE);
            } else {
                mEmptyView.setVisibility(GONE);
            }
        }
    }

    private AdapterDataObserver mDataObserver = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            if (mAdapter == null) return;


            dataChanged();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            if (mAdapter == null) return;

            dataChanged();
        }

        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            if (mAdapter == null) return;

            dataChanged();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            if (mAdapter == null) return;

            dataChanged();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            if (mAdapter == null) return;

            dataChanged();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            if (mAdapter == null) return;

            dataChanged();
        }
    };


}
